package com.dirk.happie.fragments;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.dirk.happie.R;
import com.dirk.happie.activities.MapViewActivity;
import com.dirk.happie.beans.CompanyBean;
import com.dirk.happie.utils.App;
import com.dirk.happie.utils.Common;
import com.dirk.happie.utils.Keys;
import com.dirk.happie.utils.LogoDownloader;

public class CompanyFragment extends Fragment {

	private Context context;
	
	private CompanyBean company;

	private App mApp;

	private ImageView companyLogoView;

	private LogoDownloader logoDownloader = null;
	private String cacheFolderPath = "";

	public CompanyFragment() {
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.mApp = ((App) getActivity().getApplication());

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		context = getActivity();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.company_fragment, container, false);
		company = parseCompanyJSON(getArguments().getInt("current_page"));
		cacheFolderPath = Environment.getExternalStorageDirectory().getPath() + "/happie_cache";
		File file = new File(cacheFolderPath);
		file.mkdirs();
		logoDownloader = new LogoDownloader();
		
		TextView companyNameTextView = (TextView) view.findViewById(R.id.txt_company);
		companyNameTextView.setText(company.getCompanyName());

		TextView companyCityTextView = (TextView) view.findViewById(R.id.txt_company_address);
		companyCityTextView.setText(company.getCompanyCity());

		TextView companyPhoneTextView = (TextView) view.findViewById(R.id.txt_company_phone);
		companyPhoneTextView.setText(company.getCompanyPhone());

		ProgressBar companyProgressBar = (ProgressBar) view.findViewById(R.id.logoProgress);

		ImageView phoneButton = (ImageView) view.findViewById(R.id.btn_phone);
		ImageView mapButton = (ImageView) view.findViewById(R.id.btn_map);

		companyLogoView = (ImageView) view.findViewById(R.id.img_company);
		if (mApp.getFavorites() != null && mApp.getFavorites().size() > 0 && company.getCompanyLogo() != null) {
			logoDownloader.download(Keys.LOGOS_URL + company.getCompanyLogo(), cacheFolderPath + "/" + Common.getMD5(company.getCompanyLogo()), companyLogoView);
		} else {
			companyProgressBar.setVisibility(View.GONE);
			phoneButton.setVisibility(View.GONE);
			companyLogoView.setImageResource(R.drawable.happie_logo);
		}

		if (!mApp.isLoggedIn() && mApp.getFavorites().size() <= 0) {
			LinearLayout layout = (LinearLayout) view.findViewById(R.id.company_layout);
			layout.setVisibility(View.GONE);
		}

		mapButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, MapViewActivity.class);
				intent.putExtra("lat", company.getCompanyPosX());
				intent.putExtra("lng", company.getCompanyPosY());
				startActivityForResult(intent, 10);

			}
		});

		if (company.getCompanyPhone() == null || company.getCompanyPhone().equals("")) {
			phoneButton.setVisibility(View.GONE);
		}

		phoneButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_DIAL);
				String pn = "tel:" + company.getCompanyPhone();
				intent.setData(Uri.parse(pn));
				startActivity(intent);
			}
		});

		return view;
	}

	@Override
	public void onDestroy() {
		if (logoDownloader != null) {
			logoDownloader.clearImages();
		}
		super.onDestroy();
	}

	private CompanyBean parseCompanyJSON(int currentPage) {

		CompanyBean companyBean;
		if (mApp.getFavorites() != null && mApp.getFavorites().size() > 0) {
			companyBean = mApp.getFavorites().get(currentPage);
		} else {
			companyBean = new CompanyBean();
			companyBean.setCompanyName("");
			companyBean.setCompanyCity(getString(R.string.no_favorites));
			companyBean.setCompanyPhone("");
		}
		return companyBean;
	}
}